package com.lbb.waimai.controller;

import com.lbb.waimai.entiry.Order;
import com.lbb.waimai.entiry.vo.OrderVo;
import com.lbb.waimai.result.R;
import com.lbb.waimai.service.OrderService;
import com.lbb.waimai.utils.JwtHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    //生成订单
    @PostMapping("/toOrder")
    public R toOrder(HttpServletRequest request, @RequestBody OrderVo orderVo){
        Long userId = JwtHelper.getUserId(request);
        orderService.toOrder(userId, orderVo);
        return R.ok();
    }

    //获取订单,获取该用户id下，订单
    @GetMapping("getOrder")
    public R getOrder(HttpServletRequest request){
        Long userId = JwtHelper.getUserId(request);
        List<Order> order = orderService.getOrder(userId);
        return R.ok().data("data",order);
    }

    //取消订单，实现判断当前时间是否能够取消，
    //规定取餐时间与当前时间相隔5小时无法取消，（申请取消）
    @GetMapping("/cancelOrder")
    public R cancelOrder(HttpServletRequest request,Integer id){
        Long userId = JwtHelper.getUserId(request);
        orderService.cancelOrder(id,userId);
        return R.ok();
    }

    //支付
    @GetMapping("/payOrder")
    public R payOrder(Integer id){
        orderService.payOrder(id);
        return R.ok();
    }

    //收货成功
    @GetMapping("complete")
    public R complete(Integer id){
        Order order = orderService.getById(id);
        order.setStatus(3);
        orderService.updateById(order);
        return R.ok();
    }
}
